package com.xk.androiddemo.ui;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
import com.xk.androiddemo.App;
import com.xk.androiddemo.LoginActivity;
import com.xk.androiddemo.R;
import com.xk.androiddemo.SwitchAccountsActivity;
import com.xk.androiddemo.databinding.FragmentDashboardBinding;
import com.xk.androiddemo.databinding.FragmentSettingsBinding;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class SettingsFragment extends Fragment {

    private SettingsViewModel mViewModel;
    private FragmentSettingsBinding bind;

    public static SettingsFragment newInstance() {
        return new SettingsFragment();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(SettingsViewModel.class);
        bind= FragmentSettingsBinding.inflate(getLayoutInflater());
        String str = "用户名: " + App.userBean.getName();
        bind.user.setText(str);
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentTime.format(formatter);
        bind.tvTime.setText(formattedDateTime);
        bind.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginexit();
            }
        });
        return bind.getRoot();
    }

    private void loginexit(){
            String[] menutext ={"退出登录","切换用户"};
        BottomMenu.show(menutext).setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
            @Override
            public boolean onClick(BottomMenu bottomMenu, CharSequence charSequence, int i) {
                if(i==0){
                    Intent intent =new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
                if(i==1){
                    Intent intent =new Intent(getActivity(), SwitchAccountsActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
                return false;
            }
        });
    }

}